package com.teamwork.module01.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamwork.common.entity.module01.Class;
import com.teamwork.common.entity.module01.Course;
import com.teamwork.common.result.Result;
import com.teamwork.module01.mapper.CourseMapper;
import com.teamwork.module01.mapper.daomapper.StuAndCourseMapper;
import com.teamwork.module01.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

@Resource
private StuAndCourseMapper stuAndCourseMapper;

    @Override
    public List<Course> getoneCourse(Long id) {


       log.info("学生："+id+"查询了课表。。。");

        List<Integer> courseIDs = stuAndCourseMapper.getCourseIDs(id);





        ArrayList<Course> courseList= new ArrayList<>();

        courseIDs.forEach(cid->{

            Course course = this.getById(cid);

            courseList.add(course);
        });


        return courseList;
    }


    /**
     * 根据名称获取课程信息
     * @param name
     * @return
     */
    @Override
    public Result getByName(String name) {

        LambdaQueryWrapper<Course> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(Course::getCourseName,name).or().like(Course::getCourseName,name);

        Course c = this.getOne(wrapper);
        if(Objects.isNull(c)){
            return Result.failure("该课程不存在");
        }
        return Result.success(c);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Result getPage(Integer page, Integer size) {
        Page<Course>  coursePage = new Page<>(page,size);
        this.page(coursePage);
        return Result.success(coursePage);
    }

    /**
     * 添加或者修改
     * @param course
     * @return
     */
    @Override
    public Result InsertOrUpdate(Course course) {
        if(course.getId()!=null || !course.getId().equals("")){
            //修改
            boolean b = this.updateById(course);
            if(!b) return Result.failure("修改失败");
        }
        //添加
        boolean b = this.save(course);
        if(!b) return Result.failure("添加失败");
        return Result.success("添加成功");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        return null;
    }

    /**
     * id数组批量删除
     * @param ids
     * @return
     */
    @Override
    public Result batchDelete(List<Integer> ids) {
        for (Integer id : ids) {
            boolean b = this.removeById(id);
            if(!b) return Result.failure("删除失败");
        }
       return Result.success("删除成功");
    }

}
